package com.dhh.nutrition.dao;

import java.util.ArrayList;
import java.util.List;

import com.dhh.nutrition.vo.Food;

import android.content.Context;
import android.database.Cursor;

public class FoodDAO {
	private DataBaseHelper db;
	
	public FoodDAO(Context ctx){
		db = new DataBaseHelper(ctx);
	}
	
	public FoodDAO(DataBaseHelper db){
		this.db = db;
	}
	
	public Food getFood(String fno){
		Food food = null;
		try{
			db.openDataBase();
			String whereSQL = "NDB_No='"+fno+"'";
			Cursor cursor = db.getReadableDatabase().query(true, "FOOD_DES", Food.getColumns(), whereSQL,null, null, null, null, "10");				
			if(cursor!=null){
				cursor.moveToFirst();
				do{
					food = generateFood(cursor);
				}while(cursor.moveToNext());
				cursor.close();
			}			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return food;
	}
	
	public List<Food> listFoods(String foodGroup,String foodName,int page){
		int pageCnt = 20;
		int start = pageCnt * (page-1);
		String limit = start+","+pageCnt;
		
		
		List<Food> foods = new ArrayList<Food>();
		try{
			db.openDataBase();
			String whereSQL = "1=1 ";
			if(foodGroup!=null && foodGroup.trim().length()>0){
				whereSQL += "and fdgrp_cd='"+foodGroup+"' ";
			}
			if(foodName!=null && foodName.trim().length()>0){
				whereSQL += "and shrt_desc like '"+foodName+"%' ";
			}
			Cursor cursor = db.getReadableDatabase().query(true, "FOOD_DES", Food.getColumns(), whereSQL,null, null, null, "shrt_desc", limit);				
			if(cursor!=null){
				cursor.moveToFirst();
				do{
					foods.add(generateFood(cursor));
				}while(cursor.moveToNext());
				cursor.close();
			}			
		}catch(Exception e){
			e.printStackTrace();
		}finally{
			if(db!=null) db.close();
		}
		
		return foods;
	}
	
	private Food generateFood(Cursor cursor){
		Food food = new Food();
		food.setNDB_No(cursor.getString(0));
		food.setShrt_Desc(cursor.getString(1));
		food.setWater(cursor.getDouble(2));
		food.setEnerg_Kcal(cursor.getDouble(3));
		food.setProtein(cursor.getDouble(4));
		food.setLipid_Tot(cursor.getDouble(5));
		food.setAsh(cursor.getDouble(6));
		food.setCarbohydrt(cursor.getDouble(7));
		food.setFiber_TD(cursor.getDouble(8));
		food.setSugar_Tot(cursor.getDouble(9));
		food.setCalcium(cursor.getDouble(10));
		food.setIron(cursor.getDouble(11));
		food.setMagnesium(cursor.getDouble(12));
		food.setPhosphorus(cursor.getDouble(13));
		food.setPotassium(cursor.getDouble(14));
		food.setSodium(cursor.getDouble(15));
		food.setZinc(cursor.getDouble(16));
		food.setCopper(cursor.getDouble(17));
		food.setManganese(cursor.getDouble(18));
		food.setSelenium(cursor.getDouble(19));
		food.setVit_C(cursor.getDouble(20));
		food.setThiamin(cursor.getDouble(21));
		food.setRiboflavin(cursor.getDouble(22));
		food.setNiacin(cursor.getDouble(23));
		food.setPanto_Acid(cursor.getDouble(24));
		food.setVit_B6(cursor.getDouble(25));
		food.setFolate_Tot(cursor.getDouble(26));
		food.setFolic_Acid(cursor.getDouble(27));
		food.setFood_Folate(cursor.getDouble(28));
		food.setFolate_DFE(cursor.getDouble(29));
		food.setCholine_Tot(cursor.getDouble(30));
		food.setVit_B12(cursor.getDouble(31));
		food.setVit_A_IU(cursor.getDouble(32));
		food.setVit_A_RAE(cursor.getDouble(33));
		food.setRetinol(cursor.getDouble(34));
		food.setAlpha_Carot(cursor.getDouble(35));
		food.setBeta_Carot(cursor.getDouble(36));
		food.setBeta_Crypt(cursor.getDouble(37));
		food.setLycopene(cursor.getDouble(38));
		food.setLut_Zea(cursor.getDouble(39));
		food.setVit_E(cursor.getDouble(40));
		food.setVit_D_mcg(cursor.getDouble(41));
		food.setViVit_D_IU(cursor.getDouble(42));
		food.setVit_K(cursor.getDouble(43));
		food.setFA_Sat(cursor.getDouble(44));
		food.setFA_Mono(cursor.getDouble(45));
		food.setFA_Poly(cursor.getDouble(46));
		food.setCholestrl(cursor.getDouble(47));
		food.setGmWt_1(cursor.getDouble(48));
		food.setGmWt_Desc1(cursor.getString(49));
		food.setGmWt_2(cursor.getDouble(50));
		food.setGmWt_Desc2(cursor.getString(51));
		food.setRefuse_Pct(cursor.getDouble(52));
		food.setFdGrp_Cd(cursor.getString(53));
		return food;
	}
	
}
